package com.tanhua.sso.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.sso.config.AliyunSMSConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Slf4j
@Service
public class SmsService {
    @Autowired
    private AliyunSMSConfig aliyunSMSConfig;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static final String REDIS_CODE="CHECK_CODE_";
    //@PostConstruct
    //public void test(){
    //    for (int i = 0; i < 99999999; i++) {
    //        System.out.println(aliyunSMSConfig.getAccessKeyId());
    //        try {
    //            Thread.sleep(500);
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //        }
    //    }
    //}

    /**
     * 发送短信
     * @param mobile
     * @return
     */
    public String sendSms(String mobile) {

        String code = RandomUtil.randomNumbers(6);

        try {
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(aliyunSMSConfig.getAccessKeyId())
                    // 您的AccessKey Secret
                    .setAccessKeySecret(aliyunSMSConfig.getAccessKeySecret())
                    .setEndpoint(aliyunSMSConfig.getDomain());
            // 访问的域名
            Client client = new Client(config);

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile) //目标手机号
                    .setSignName(aliyunSMSConfig.getSignName()) //签名名称
                    .setTemplateCode(aliyunSMSConfig.getTemplateCode()) //短信模板code
                    .setTemplateParam("{\"code\":\"" + code + "\"}"); //模板中变量替换
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            if (StrUtil.equals(sendSmsResponse.getBody().getCode(),"OK")){
                return code;
            }
            SendSmsResponseBody body = sendSmsResponse.getBody();
        } catch (Exception e) {
            log.error("发送短信验证码失败!mobile={}",mobile,e);
        }
        return null;
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    public Object sendCheckCode(String phone) {
        String redisKey = REDIS_CODE + phone;
        //先判断改手机号的验证码是否过期 如果没过期 那么不能发送
        if (stringRedisTemplate.hasKey(redisKey)){
            return ErrorResult.builder().errCode("500").errMessage("抱歉验证码还未过期,请稍后再试").build();
        }

        //String code = this.sendSms(phone);
        String code = "123456";
        //发送失败
        if (null==code){
            return ErrorResult.builder().errCode("500").errMessage("抱歉验证码发送失败").build();
        }
        //发送成功
        this.stringRedisTemplate.opsForValue().set(redisKey,code, Duration.ofMinutes(5));
        return null;
    }
}
